package com.ideal.frame.dto;

import com.ideal.frame.jpa.DSLPageRequest;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;

@Getter
@Setter
public class SearchForm {
    private Integer pageSize = 10;
    private Integer pageNo = 1;
    private String sort;
    private String order;

    public DSLPageRequest asDslPageRequest() {
        DSLPageRequest dslPageRequest = new DSLPageRequest(this.pageNo - 1, this.pageSize);
        if (StringUtils.isNotEmpty(this.order)) {
            Sort.Order sortOrder = new Sort.Order(StringUtils.equals(this.sort, "asc") ? Sort.Direction.ASC : Sort.Direction.DESC, this.order);
            dslPageRequest.addOrderBy(sortOrder);
        }

        return dslPageRequest;
    }
}
